/*
 * Copyright (c) 2014, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.omorphdb.core.value;

import java.util.Date;

import org.omorphdb.core.io.Constants;

/**
 * Set of convenient methods to create a value object of desired type.
 * 
 * @author Marco Tamburelli
 */
public final class ValueFactory
{
	/*
	 * FIXME The size of each valeu is the real value + 1 which is the byte
	 * identifying the type. I don't know if it wouldn't be better to provide
	 * only the size of the value and add this "1" where is required.
	 */
	public static Value<Boolean> createBoolean(Boolean b)
	{
		return new PrimitiveValue<Boolean>(b, ValueType.BOOLEAN, Constants.SIZEOF_BOOLEAN + 1);
	}

	public static Value<Byte> createByte(Byte b)
	{
		return new PrimitiveValue<Byte>(b, ValueType.BYTE, Constants.SIZEOF_BYTE + 1);
	}

	public static Value<Character> createChar(Character b)
	{
		return new PrimitiveValue<Character>(b, ValueType.CHAR, Constants.SIZEOF_CHAR + 1);
	}

	public static Value<Short> createShort(Short s)
	{
		return new PrimitiveValue<Short>(s, ValueType.SHORT, Constants.SIZEOF_SHORT + 1);
	}

	public static Value<Integer> createInteger(Integer i)
	{
		return new PrimitiveValue<Integer>(i, ValueType.INT, Constants.SIZEOF_INT + 1);
	}

	public static Value<Float> createFloat(Float f)
	{
		return new PrimitiveValue<Float>(f, ValueType.FLOAT, Constants.SIZEOF_FLOAT + 1);
	}

	public static Value<Long> createLong(Long l)
	{
		return new PrimitiveValue<Long>(l, ValueType.LONG, Constants.SIZEOF_LONG + 1);
	}

	public static Value<Date> createDate(Long l)
	{
		return new PrimitiveValue<Date>(new Date(l), ValueType.DATE, Constants.SIZEOF_LONG + 1);
	}

	public static Value<Double> createDouble(Double d)
	{
		return new PrimitiveValue<Double>(d, ValueType.DOUBLE, Constants.SIZEOF_DOUBLE + 1);
	}

	public static Value<String> createString(byte[] ba)
	{
		return new PrimitiveValue<String>(new String(ba), ValueType.STRING, Constants.SIZEOF_INT + ba.length + 1);
	}

	public static Value<String> createString(String str)
	{
		return new PrimitiveValue<String>(str, ValueType.STRING, Constants.SIZEOF_INT + str.getBytes().length + 1);
	}
}
